import path from 'path'
import log4js from 'koa-log4'


log4js.configure({
 appenders: {
  access: {
   type: 'dateFile',
   pattern: '-yyyy-MM-dd.log', //生成文件的规则
   filename: path.join('back-end/logs/', 'access.log'), //生成文件名
   daysToKeep: 7
  },
  application: {
   type: 'dateFile',
   pattern: '-yyyy-MM-dd.log',
   filename: path.join('back-end/logs/', 'application.log'),
   daysToKeep: 7
  },
  out: {
   type: 'console'
  },
    database: {
        type: "dateFile",
        pattern: "-yyyy-MM-dd.log",
        filename: path.join('back-end/logs/db','db.log'),
        daysToKeep: 7
    },
 },
 categories: {
  default: { appenders: [ 'out' ], level: 'info' },
  access: { appenders: [ 'access' ], level: 'info' },
  application: { appenders: [ 'application','out' ], level: 'info'},
  db: {appenders:['database'],level:"info"}
 }
});

export const accessLogger = () => log4js.koaLogger(log4js.getLogger('access')); //记录所有访问级别的日志
export const logger = log4js.getLogger('application'); //记录所有应用级别的日志
export const httpLogger = log4js.getLogger("access") // 记录网络请求
export const dbLogger = log4js.getLogger("db")
export const formatText = {
    info: function(info) {
        var logText = new String();
        //响应日志头信息
        logText += "\n" + "***************info log start ***************" + "\n";
        //响应内容
        logText += "info detail: " + "\n" + JSON.stringify(info) + "\n";
        //响应日志结束信息
        logText += "*************** info log end ***************" + "\n";
        return logText;
    },
    request: function(req, resTime) {
        var logText = new String();
        var method = req.method;
        //访问方法
        logText += "request method: " + method + "\n";
        //请求原始地址
        logText += "request originalUrl:  " + req.originalUrl + "\n";
        //客户端ip
        logText += "request client ip:  " + req.ip + "\n";
        //开始时间
        var startTime;
        //请求参数
        if (method === 'GET') {
            logText += "request query:  " + JSON.stringify(req.query) + "\n";
            // startTime = req.query.requestStartTime;
        } else {
            logText += "request body: " + "\n" + JSON.stringify(req.body) + "\n";
            // startTime = req.body.requestStartTime;
        }
        //服务器响应时间
        logText += "response time: " + resTime + "\n";
        return logText;
    },
    response: function(ctx, resTime) {
        var logText = new String();
        //响应日志开始
        logText += "\n" + "*************** response log start ***************" + "\n";
        //添加请求日志
        logText += formatText.request(ctx.request, resTime) as string;
        //响应状态码
        logText += "response status: " + ctx.status + "\n";
        //响应内容
        logText += "response body: " + "\n" + JSON.stringify(ctx.body) + "\n";
        //响应日志结束
        logText += "*************** response log end ***************" + "\n";
        return logText;
    },
    handle: function(info) {
        var logText = new String();
        //响应日志开始
        logText += "\n" + "***************info log start ***************" + "\n";
        //响应内容
        logText += "handle info detail: " + "\n" + JSON.stringify(info).replace(/\\n/g, "\n") + "\n";
        //响应日志结束
        logText += "*************** info log end ***************" + "\n";
        return logText;
    },
    error: function(ctx, err, resTime) {
        var logText = new String();
        //错误信息开始
        logText += "\n" + "*************** error log start ***************" + "\n";
        //添加请求日志
        logText += formatText.request(ctx.request, resTime) as string;
        //错误名称
        logText += "err name: " + err.name + "\n";
        //错误信息
        logText += "err message: " + err.message + "\n";
        //错误详情
        logText += "err stack: " + err.stack + "\n";
        //错误信息结束
        logText += "*************** error log end ***************" + "\n";
        return logText;
    }
}
